package NursE;

import lejos.nxt.LCD;
import lejos.nxt.NXTRegulatedMotor;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
import lejos.nxt.UltrasonicSensor;

public class NEHead {

	/**
	 * ----------------------------------------------------------------
	 * 								Attributes
	 * ----------------------------------------------------------------
	 */
	
	protected NXTRegulatedMotor motor;
	protected UltrasonicSensor eyes;
	/**
	 * ----------------------------------------------------------------
	 * 								Static constructor
	 * ----------------------------------------------------------------
	 */
	
	public NEHead(NXTRegulatedMotor motor,SensorPort port)
	{
		this.setMotor(motor);
		this.setUltrasonic(port);
	}
	
	
	/**
	 * ----------------------------------------------------------------
	 * 								Setters
	 * ----------------------------------------------------------------
	 */
	
	public void setMotor(NXTRegulatedMotor motor) {
		this.motor = motor;
		this.motor.setSpeed(50);	
	}
	
	public void setUltrasonic(SensorPort port) {
		this.eyes = new UltrasonicSensor(port);
		
	}
	
	/**
	 * ----------------------------------------------------------------
	 * 								Action
	 * ----------------------------------------------------------------
	 */
		
	public void lookToTheRight()
	{
		this.motor.rotate(-90);
	}
	
	public void lookToTheLeft()
	{
		this.motor.rotate(90);
	}
	
	//regarde s'il y a un obstacle � droite
	public boolean obstacleToTheRight(int distance)
	{
		boolean obstacle;
		this.lookToTheRight();
		obstacle= this.detection(distance);
		this.lookToTheFront(false);
	
		return obstacle;
		
	}
	
	//regarde s'il y a un obstacle � gauche
	public boolean obstacleToTheLeft(int distance)
	{
		
		
		boolean obstacle;
		this.lookToTheLeft();
		obstacle= this.detection(distance);
		this.lookToTheFront(true);
			return obstacle;
		
	}
	
	//remet la t�te droite
	public void lookToTheFront(boolean headAtLeft)
	{
		if(headAtLeft==true){
			this.motor.rotate(-90);
		}
		else{
			this.motor.rotate(90);
		}
	}
	
	public boolean detectToTheFront(boolean headAtLeft)
	{
		lookToTheFront(headAtLeft);
		return detection(15);
	
	}
		
	//d�tecte s'il y a un obstacle � la distance indiqu�e
	public boolean detection(int distance)
	{		
		return (this.eyes.getDistance()<=distance);
	}
}
